
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>计算器</title>
</head>
<body>
<table style="margin: 0 auto;text-align: center">
    <tr>
        <td colspan="4"><h2>计算</h2></td>
    </tr>
    <tr>
        <td>请输入：</td>
        <td colspan="3"><input type="text" id="x"></td>
    </tr>
    <tr>
        <td>请输入：</td>
        <td colspan="3"><input type="text" id="y"></td>
    </tr>
    <tr>
        <td>结果：</td>
        <td colspan="3"><div id="z"></div></td>
    </tr>
    <tr>
        <td><button type="button" onclick="sum()">+</button></td>
        <td><button type="button" onclick="jian()">-</button></td>
        <td><button type="button" onclick="cheng()">x</button></td>
        <td><button type="button" onclick="chu()">/</button></td>
    </tr>
</table>

<script>
    function sum() {
        var x=parseFloat(document.getElementById("x").value);
        var y=parseFloat(document.getElementById("y").value);
        document.getElementById("z").innerHTML=(x+y).toString();
    }
    function jian() {
        var x=parseFloat(document.getElementById("x").value);
        var y=parseFloat(document.getElementById("y").value);
        document.getElementById("z").innerHTML=(x-y).toString();
    }
    function cheng() {
        var x=parseFloat(document.getElementById("x").value);
        var y=parseFloat(document.getElementById("y").value);
        document.getElementById("z").innerHTML=(x*y).toString();
    }
    function chu() {
        var x=parseFloat(document.getElementById("x").value);
        var y=parseFloat(document.getElementById("y").value);
        if (y===0){
            document.getElementById("z").innerHTML="除数不能为零！";
        }else
        document.getElementById("z").innerHTML=(x/y).toString();
    }
</script>
</body>
</html>
